package com.example.healthapp.ui.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.healthapp.R;
import com.example.healthapp.utils.InterfaceTool;
import com.example.healthapp.utils.SPManage;

// 心率页面的Fragment
public class HeartRateActivity extends AppCompatActivity {

    EditText heart_count;
    ImageView heart_back, heart_image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_heart_rate);
        InterfaceTool.setFullScreen(this, true);
        intView();
        refreshView();
        event();
    }

    private void event() {
        heart_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        heart_count.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_DONE) {
                    Toast.makeText(HeartRateActivity.this, "录入成功", Toast.LENGTH_SHORT).show();
                    SPManage.getSP(HeartRateActivity.this, "health").putString("heart", heart_count.getText().toString()).commit();
                    return true;
                }
                return false;
            }
        });
    }

    private void refreshView() {
        String heart = SPManage.getSP(this, "health").getString("heart");
        heart_count.setText(heart);
        InterfaceTool.setAnimator(heart_image, new float[]{0}, new float[]{0}, new float[]{0.9f, 1.2f, 1f, 0.9f}, new float[]{0.9f, 1.2f, 1f, 0.9f}, new float[]{0.8f, 1f, 0.8f}, new float[]{0}, 1000, -1);
    }

    private void intView() {
        heart_count = findViewById(R.id.heart_count);
        heart_back = findViewById(R.id.heart_back);
        heart_image = findViewById(R.id.heart_image);
    }
}